package com.huayue.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.SaleOrder;
import com.huayue.entity.SaleOrderDetails;
import com.huayue.entity.User;
import com.huayue.service.ProductService;
import com.huayue.service.SaleOrderService;
import com.huayue.util.OrderNumberHelper;
import com.huayue.util.PageHelper;

/**
 * 销售订单
 * 
 */
@Controller
@RequestMapping("/saleOrder")
public class SaleOrderController {

    @Autowired
    SaleOrderService svc;
    @Autowired
    ProductService proSvc;
    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(SaleOrder saleOrder) {
        FastJsonJsonView view = new FastJsonJsonView();
        // 根据前台判断是否需要生成订单号
        if ("true".equals(saleOrder.getOrderNumber())) {
            view.addStaticAttribute("orderNumber", OrderNumberHelper.getOrderNumber(OrderNumberHelper.XSDORDER, "XSD"));
        }
        view.addStaticAttribute("product", proSvc.findAllProduct());
        return new ModelAndView(view);
    }

    /**
     * 清除订单号
     *
     * @param saleOrder
     * @return
     */
    @RequestMapping("/close")
    public ModelAndView close(SaleOrder saleOrder) {
        FastJsonJsonView view = new FastJsonJsonView();
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.XSDORDER, saleOrder.getOrderNumber());
        view.addStaticAttribute("code", 0);
        return new ModelAndView(view);
    }

    @RequestMapping("/findDetails")
    public ModelAndView findDetails(String id) {
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        SaleOrder saleOrder = new SaleOrder();
        saleOrder.setId(Long.valueOf(id));
        view.addStaticAttribute("order", svc.findOne(saleOrder));
        view.addStaticAttribute("data", svc.findDetails(id));
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-销售订单")
    @RequestMapping("/add")
    public ModelAndView add(@RequestBody String json, HttpServletRequest request) {
        // 获取session中该用户信息
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("USER");
        // 格式化json数据
        JSONObject jsonData = JSONObject.parseObject(json);
        // 创建订单信息
        SaleOrder order = new SaleOrder();
        order.setEmployeeId(Long.valueOf(user.getId()));
        order.setOrderNumber(String.valueOf(jsonData.get("orderNumber")));
        order.setRemark(jsonData.get("remark").toString());
        JSONArray jsonArray = jsonData.getJSONArray("table");
        // 获取订单详情
        List<SaleOrderDetails> details = JSONArray.parseArray(jsonArray.toString(), SaleOrderDetails.class);
        // 将订单信息和订单详情传至service进行处理
        boolean result = svc.addDetails(order, details);
        // 将临时订单号销毁
        OrderNumberHelper.removeOrderNumber(OrderNumberHelper.XSDORDER, order.getOrderNumber());
        // 返回数据给页面
        FastJsonJsonView view = new FastJsonJsonView();
        view.addStaticAttribute("code", 0);
        if (result) {
            view.addStaticAttribute("icon", 1);
            view.addStaticAttribute("msg", "订单添加成功！");
        } else {
            view.addStaticAttribute("icon", 2);
            view.addStaticAttribute("msg", "订单添加失败！");
        }
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "修改-销售订单")
    @RequestMapping("/update")
    public ModelAndView update(SaleOrder saleOrder) {
        return bc.update(svc, saleOrder);
    }

    @LogAnno(operateType = "删除-销售订单")
    @RequestMapping("/delete")
    public ModelAndView delete(SaleOrder saleOrder) {
        return bc.delete(svc, saleOrder);
    }

    @RequestMapping(value = "/findOne", produces = {"text/javascript;charset=UTF-8"})
    public ModelAndView findOne(SaleOrder saleOrder) {
        return bc.findOne(svc, saleOrder);
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page, SaleOrder saleOrder) {
        return bc.findPage(svc, page, saleOrder, saleOrder);
    }

}
